package com.cvit.cn.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cvit.cn.bean.Msg;
import com.cvit.cn.bean.SupplierInfo;
import com.cvit.cn.service.SupplierInfoService;
import com.cvit.cn.test.ObjectExcelView;
import com.cvit.cn.test.UuidUtil;
/**
 * @className:
  * @author LX
 *
 */
@Controller
public class SupplierInfoController {
	
	@Autowired
	SupplierInfoService supplierInfoService;
	
	@RequestMapping("qqqid")
	@ResponseBody
	public Msg addVendorsById(String id){
		List<SupplierInfo> si=supplierInfoService.addVendorsById(id);
		//if(sq.size()>0){
			return Msg.success().add("success", si);
	}
	
	
	/**
	 * 根据supplierEmail给flag赋值为1
	 * */
	@ResponseBody
	@RequestMapping("/updateFlag")
	public Msg updateFlag(@RequestParam("supplierEmail")String supplierEmail){
		SupplierInfo si = new SupplierInfo();
		si.setSupplierEmail(supplierEmail);
		si.setFlag(1);
		supplierInfoService.updateFlag(si);
		return Msg.success();
	}
	
	/**添加供应商
	 * 
	 */
	@RequestMapping(value="/addSupplierInfo")
	@ResponseBody
	public Msg addSupplierInfo(String supplierName,String management,String supplierEmail,String supplierPostcodes,String contacts,String post,String phone,String address){
		SupplierInfo addSupplierInfo = new SupplierInfo();
		addSupplierInfo.setId(UuidUtil.get32NUMUUID());
		addSupplierInfo.setSupplierName(supplierName);
		addSupplierInfo.setManagement(management);
		addSupplierInfo.setSupplierEmail(supplierEmail);
		addSupplierInfo.setSupplierPostcodes(supplierPostcodes);
		addSupplierInfo.setContacts(contacts);
		addSupplierInfo.setPost(post);
		addSupplierInfo.setPhone(phone);
		addSupplierInfo.setAddress(address);
		addSupplierInfo.setFlag(0);
		int i = supplierInfoService.addSupplierInfo(addSupplierInfo);
		if(i!=0){
			return Msg.success().add("id",addSupplierInfo.getId()).add("email", addSupplierInfo.getSupplierEmail());
		}else{
			return Msg.fail().add("fail", "添加供应商失败！！！");
		}
	}
	
	/**
	 * 删除供应商
	 */
	@RequestMapping(value="/delSupplierInfo")
	@ResponseBody
	public Msg delSupplierInfoTest(String id){
		String[] ids = id.split(",");
		if(supplierInfoService.delSupplierInfo(ids)!=0){
			return Msg.success().add("success", "删除供应商成功");
		}else{
			return Msg.fail().add("fail", "删除供应商失败");
		}
	} 
	@RequestMapping("/deleSupplier")
	@ResponseBody
	public Msg deleSupplier(@RequestParam("id")String id){
		supplierInfoService.deleSupplier(id);
		return Msg.success();
	}
	
	/**
	 * 修改供应商
	 */
	@RequestMapping(value="/updateSupplierInfo")
	@ResponseBody
	public Msg updateSupplierInfo(String id,String supplierName,String management,String supplierEmail,String supplierPostcodes,String contacts,String post,String phone,String address){
		SupplierInfo updateSupplierInfo = new SupplierInfo();
		updateSupplierInfo.setId(id);
		updateSupplierInfo.setSupplierName(supplierName);
		updateSupplierInfo.setManagement(management);
		updateSupplierInfo.setSupplierEmail(supplierEmail);
		updateSupplierInfo.setSupplierPostcodes(supplierPostcodes);
		updateSupplierInfo.setContacts(contacts);
		updateSupplierInfo.setPost(post);
		updateSupplierInfo.setPhone(phone);
		updateSupplierInfo.setAddress(address);
		updateSupplierInfo.setFlag(0);
		if(supplierInfoService.updateSupplierInfo(updateSupplierInfo)!=0){
			return Msg.success().add("success", "修改供应商成功");
		}else{
			return Msg.fail().add("fail", "修改供应商失败");
		}
	}
	
	/**
	 * 查询供应商
	 */
	@RequestMapping(value="/selectSupplierInfo")
	@ResponseBody
	public Msg selectSupplierInfo(){
		//查询返回多条数据 放list里面
			List<SupplierInfo> list=supplierInfoService.selectSupplierInfo();
			if(list!=null){
			return Msg.success().add("data", list);
			}else{
			return Msg.fail().add("fail", "查询供应商失败");
		}
	}
	
	/**
	 * 假删除
	 */
	@RequestMapping(value="/delSupplierInfoFalse")
	@ResponseBody
	public Msg delSupplierInfoFalse(String id){
		String[] ids = id.split(",");
		if(supplierInfoService.delSupplierInfoFalse(ids)!=0){
			return Msg.success().add("success", "假删除供应商成功");
		}else{
			return Msg.fail().add("fail", "假删除供应商成功");
		}
	}
	
	//------根据Id查询
	@RequestMapping(value="/selectBySupplierInfoId")
	@ResponseBody
	public Msg selectByPrimaryKey(String id){
		List<SupplierInfo> list = supplierInfoService.selectByPrimaryKey(id);
		if(list.size()>0){
			return Msg.success().add("success", list);
		}else{
			return Msg.fail().add("fail", "没有数据！！！");
		}
	}
	
	//------模糊查询
		@RequestMapping(value="/selectBySupplierName")
		@ResponseBody
		public Msg selectBySupplierName(String supplierName){
			List<SupplierInfo> list = supplierInfoService.selectBySupplierName(supplierName);
			if(list.size()>0){
				return Msg.success().add("success", list);
			}else{
				return Msg.fail().add("fail", "没有数据！！！");
			}
		}
	
	/**
	 * 根据供应商ID查所有数据
	 */
	@RequestMapping(value="/selectByInfoIdAll")
	@ResponseBody
	public Msg selectByInfoIdAll(String id){
		List list = supplierInfoService.selectByInfoIdAll(id);
		return Msg.success().add("success", list);
	}
}
